package com.tarena.csmall.security.filter;

import java.io.IOException;
import java.security.Security;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

@Component
@Order(Integer.MIN_VALUE)
public class FilterBean02 implements Filter {
    @Override public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain) throws IOException, ServletException {
        SecurityContext context = SecurityContextHolder.createEmptyContext();
        List<GrantedAuthority> authorities=new ArrayList<>();
        authorities.add(new SimpleGrantedAuthority("read"));
        UsernamePasswordAuthenticationToken token=new UsernamePasswordAuthenticationToken(
            "zhangsan",null,authorities);
        context.setAuthentication(token);
        SecurityContextHolder.setContext(context);
        HttpServletRequest httpServletReuqest=(HttpServletRequest) request;
        String uri = httpServletReuqest.getRequestURI();
        System.out.println("过滤器02请求路径uri:"+uri);
        System.out.println("过滤器02所有请求都会经过这个有效的过滤器,从而经过这个方法");
        chain.doFilter(request,response);
    }
}
